//
//  TestViewController.swift
//  JPWords
//
//  Created by Miracolo Bosco on 2025/7/8.
//

import UIKit
import SnapKit

class TestViewController: UIViewController {

    lazy var wordListCard: WordListCard = {
        let card = WordListCard()
        card.onTapAddButton = { [weak self] in
            self?.showCreateWalkmanWordList()
        }       
        return card
    }()

    lazy var misstakeCard: MisstakeCard = {
        let card = MisstakeCard()
        return card
    }()

    lazy var walkmanCard: WalkmanCard = {
        let card = WalkmanCard()
        card.onTapAddButton = { [weak self] in
            self?.showCreateWalkmanWordList()
        }
        return card
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigation()
        setupUI()
    }
    

    func setupNavigation() {
        
    }   

    func setupUI() {
        view.backgroundColor = UIColor(hexString: "#F4F4F4")
        
        let freeTestTitle = UnderlineTitleView(title: "自由测试", underlineColor: UIColor(hexString: "#52B9FB"), underlineHeight: 4)
        view.addSubview(freeTestTitle)
        freeTestTitle.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(12)
            make.centerX.equalToSuperview()
        }

        let separator = UIView()
        separator.backgroundColor = UIColor(hexString: "#D9D9D9")
        view.addSubview(separator)

        let walkmanTitle = UnderlineTitleView(title: "随身听", underlineColor: UIColor(hexString: "#5E62AC"), underlineHeight: 4)
        view.addSubview(walkmanTitle)

        let stackView = UIStackView(arrangedSubviews: [wordListCard, misstakeCard])
        stackView.axis = .horizontal
        stackView.spacing = 18
        view.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(freeTestTitle.snp.bottom).offset(24)
            make.horizontalEdges.equalToSuperview().inset(16)
        }

        separator.snp.makeConstraints { make in
            make.top.equalTo(stackView.snp.bottom).offset(38)
            make.horizontalEdges.equalToSuperview().inset(16)
            make.height.equalTo(1)
        }

        view.addSubview(walkmanCard)
        walkmanTitle.snp.makeConstraints { make in
            make.top.equalTo(separator.snp.bottom).offset(24)
            make.centerX.equalToSuperview()
        }

        walkmanCard.snp.makeConstraints { make in
            make.top.equalTo(walkmanTitle.snp.bottom).offset(24)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
    }

    func showCreateWalkmanWordList() {
//        let vc = CreateWalkmanWordListViewController()
        // let vc = ReportViewController(title: "本次学习数据报告")
        // let vc = WordsCollectionViewController()
        let vc = WordsCollectionListViewController()
        navigationController?.pushViewController(vc, animated: true)
        
//        let vc = WalkmanPlayerViewController()
//        vc.modalPresentationStyle = .fullScreen
//        present(vc, animated: true)
    }
}



class WordListCard: BaseCard {
    var onTapAddButton: (() -> Void)?
    var onTapWordList: (() -> Void)?
    
    override var bgColor: UIColor? {
        get { UIColor(hexString: "#52B9FB") }
        set { }
    }
    
    override func setupUI() {
        super.setupUI()

        let icon = UIImageView(image: UIImage(named: "btn_icon_word_list"))
        icon.contentMode = .scaleAspectFit
        addSubview(icon)

        let button = UIButton(type: .system)
        button.backgroundColor = UIColor(hexString: "#FFFFFF")
        button.layer.cornerRadius = 17
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .heavy)
        button.setTitle("添加词单", for: .normal)
        button.setTitleColor(UIColor(hexString: "#52B9FB"), for: .normal)
        button.setImage(UIImage(named: "btn_icon_add_blue")?.withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(addWordList), for: .touchUpInside)
        addSubview(button)
        
        let pageControl = UIImageView(image: UIImage(named: "page_control_add"))
        addSubview(pageControl)

        icon.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(22)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(85)
        }
        
        button.snp.makeConstraints { make in
            make.top.equalTo(icon.snp.bottom).offset(11)
            make.centerX.equalToSuperview()
            make.width.equalTo(126)
            make.height.equalTo(34)
        }
        
        pageControl.snp.makeConstraints { make in
            make.bottom.equalTo(self).inset(10)
            make.centerX.equalToSuperview()
        }
        
        snp.makeConstraints { make in
            make.width.equalTo(229)
            make.height.equalTo(181)
        }
        
    }
    
    @objc func addWordList() {
        onTapAddButton?()
    }
}

class MisstakeCard: BaseCard {
    override func setupUI() {
        super.setupUI()

        let diffLabel = UILabel()
        diffLabel.text = "10"
        diffLabel.textColor = UIColor(hexString: "#FE4A4B")
        diffLabel.font = UIFont.systemFont(ofSize: 13, weight: .bold)
        addSubview(diffLabel)

        let countLabel = UILabel()
        countLabel.text = "0"
        countLabel.textColor = UIColor(hexString: "#2F2D51")
        countLabel.font = UIFont.systemFont(ofSize: 36, weight: .semibold)
        addSubview(countLabel)

        let titleLabel = UILabel()
        titleLabel.text = "错词本"
        titleLabel.textColor = UIColor(hexString: "#2F2D51")
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        addSubview(titleLabel)

        let icon = UIImageView(image: UIImage(named: "btn_arrow_round_black"))
        icon.contentMode = .scaleAspectFit
        addSubview(icon)
        
        diffLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.left.equalTo(countLabel.snp.right)
        }

        countLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(21)
            make.left.equalToSuperview().offset(12)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(countLabel.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
        }

        icon.snp.makeConstraints { make in
            make.bottom.equalToSuperview().inset(36)
            make.centerX.equalToSuperview()
        }
    }
}

class WalkmanCard: BaseCard {
    
    var onTapAddButton: (() -> Void)?
    var onTapWordList: (() -> Void)?
    
    override var bgColor: UIColor? {
        get { UIColor(hexString: "#5E62AC") }
        set { }
    }

    override func setupUI() {
        super.setupUI()

        let icon = UIImageView(image: UIImage(named: "bg_cd_cover"))
        icon.contentMode = .scaleAspectFit
        addSubview(icon)

        icon.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(18)
            make.width.height.equalTo(117)
            make.centerY.equalToSuperview()
        }

        let bgIcon = UIImageView(image: UIImage(named: "bg_icon_music_sign"))
        bgIcon.contentMode = .scaleAspectFit
        addSubview(bgIcon)

        bgIcon.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(11)
            make.width.height.equalTo(117)
        }

        let button = UIButton(type: .system)
        button.backgroundColor = UIColor(hexString: "#FFFFFF")
        button.layer.cornerRadius = 17
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .heavy)
        button.setTitle("添加词单", for: .normal)
        button.setTitleColor(UIColor(hexString: "#5E62AC"), for: .normal)
        button.setImage(UIImage(named: "btn_icon_add_purple")?.withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(addWordList), for: .touchUpInside)
        addSubview(button)

        button.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(45)
            make.width.equalTo(126)
            make.height.equalTo(34)
        }

        let pageControl = UIImageView(image: UIImage(named: "page_control_add"))
        addSubview(pageControl)

        pageControl.snp.makeConstraints { make in
            make.bottom.equalToSuperview().inset(10)
            make.centerX.equalToSuperview()
        }
        
        snp.makeConstraints { make in
            make.height.equalTo(181)
        }
    }

    @objc func addWordList() {
        onTapAddButton?()
    }
}

class UnderlineTitleView: UIView {
    let title: String
    let underlineColor: UIColor?
    let underlineHeight: CGFloat

    init(title: String, underlineColor: UIColor?, underlineHeight: CGFloat = 4) {
        self.title = title
        self.underlineColor = underlineColor
        self.underlineHeight = underlineHeight
        super.init(frame: .zero)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        let label = UILabel()
        label.text = title
        label.textColor = UIColor(hexString: "#CCCCCC")
        label.font = UIFont.systemFont(ofSize: 24, weight: .bold)
        label.textAlignment = .center
        addSubview(label)

        let underline = UIView()
        underline.backgroundColor = underlineColor
        underline.layer.cornerRadius = underlineHeight / 2
        underline.layer.shadowColor = underlineColor?.withAlphaComponent(0.35).cgColor
        underline.layer.shadowOffset = CGSize(width: 0, height: 4)
        underline.layer.shadowOpacity = 1
        underline.layer.shadowRadius = 5
        underline.layer.masksToBounds = false
        addSubview(underline)

        label.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
        }

        underline.snp.makeConstraints { make in
            make.top.equalTo(label.snp.bottom).offset(2)
            make.bottom.equalToSuperview()
            make.horizontalEdges.equalToSuperview().inset(12)
            make.height.equalTo(underlineHeight)
        }
    }
}


